
package com.mcxx.formulcount.gonshi;

public class OperateSymbol {

    public OperateSymbol(String symb, int prio)
    {
        symbol = symb;
        priority = prio;
        digNum = 2;
    }
    
    protected String symbol;
    public String getSymbol(){
    	return this.symbol;
    }
    
    protected int priority;
    public int getPriority(){
    	return this.priority;
    }


    
    protected int digNum;
    public int getDigNum(){
    	return this.digNum;
    }
    public void setDigNum(int dig){
    	this.digNum=dig;
    }


    
    protected boolean isRight;
    public boolean  getIsRight(){
    	return this.isRight;
    }

    public  String toString()
    {
        return symbol;
    }
    public  Object  Caluc(Object pA, Object... B) throws Exception
    {
    	if (symbol.equals( ",")) {
            return pA;
        }

        double A = Double.parseDouble(pA.toString());
        if (symbol.equals("+")) {
            return A + Double.parseDouble(B[0].toString());
        }
        if (symbol.equals("-")) {
            return A -  Double.parseDouble(B[0].toString());
        }
        if (symbol.equals( "*")) {
            return A *  Double.parseDouble(B[0].toString());
        }
        if (symbol.equals( "/")) {
            return A /  Double.parseDouble(B[0].toString());
        }
        if (symbol.equals( "%")) {
            return A / 100.0;
        }

        if (symbol.equals("[roundUp]"))
        {
            Double num =Double.parseDouble(B[0].toString());
            int t =Integer.parseInt(B[1].toString());
            double cn = 1;
            for (int i = 0; i < t; i++) {
                cn *= 10;
            }
            long mid = (long)(num * cn);
            
            if (num * cn * 10 % 10 > 0) {
                return mid + 1 / cn;
            }
            return mid / cn;
        }
        if (symbol == "[round]")
        {
            Double num =Double.parseDouble(pA.toString());
            int t =(int)Double.parseDouble(B[0].toString());
            double cn = 1;
            for (int i = 0; i < t; i++) {
                cn *= 10;
            }
            
            int swr = (int)(num * cn * 10 % 10);
            int add = 0;
            if (swr >= 5) {
                add = 1;
            }
            long mid = (long)(num * cn);
            return (mid+add) / cn;
        }
        if (symbol == "[VComp]") 
        {
            if (A -  Double.parseDouble(B[0].toString())>0) {
                return A;
            } else {
                return  Double.parseDouble(B[1].toString());
            }
        }
        //if (symbol.equals( "[如果]")) return A == 1 ? Double.parseDouble(B[0].toString()) :Double.parseDouble(B[1].toString());
        throw new Exception("不支持的运算符:"+symbol);
    }
}
